/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.LoaiMonAn;
import UTIL.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ChanhTin
 */
public class LoaiMonAnDAO {
     
    /**
     * @desc lay danh sach loai mon an
     * @return 
     */
    public static List<LoaiMonAn> layDanhSach() {
        List<LoaiMonAn> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select lma from LoaiMonAn lma";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;  
    }
    
    /**
     * @desc tim loai mon an theo ma loai mon an
     * @param maLoaiMonAn
     * @return 
     */
    public static LoaiMonAn layThongTinLoaiMonAn(int maLoaiMonAn) {
        LoaiMonAn lma = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            lma = (LoaiMonAn) session.get(LoaiMonAn.class, maLoaiMonAn);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return lma;
    }
    
    /**
     * @desc them loai mon an
     * @param lma
     * @return 
     */
    public static boolean themLoaiMonAn(LoaiMonAn lma) throws HibernateException{
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (LoaiMonAnDAO.layThongTinLoaiMonAn(lma.getMaLoaiMonAn())!=null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.save(lma); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag;
    }
    
    /**
     * @desc cap nhat thong tin loai mon an
     * @param lma
     * @return 
     */
    public static boolean capNhatThongTinLoaiMonAn(LoaiMonAn lma) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (LoaiMonAnDAO.layThongTinLoaiMonAn(lma.getMaLoaiMonAn()) == null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.update(lma); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
           //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    } 
    
    /**
     * @desc xoa loai mon an
     * @param maLoaiMonAn
     * @return 
     */
    public static boolean xoaLoaiMonAn(int maLoaiMonAn) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        LoaiMonAn lma = LoaiMonAnDAO.layThongTinLoaiMonAn(maLoaiMonAn); 
        if(lma==null){ 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.delete(lma); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    }
}
